<!DOCTYPE html>
<html>
<head>
    <title>Charge Distribution Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="800" height="300" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.fillStyle = 'black';
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1.5;
    ctx.textAlign = 'center';

    // Coordinates and scaling
    const centerX = canvas.width / 2;
    const centerY = canvas.height / 2 - 20; // Shift up to make space for caption
    const a_dist = 120; // Pixel distance for 'a'
    const tickHeight = 8;
    const labelOffsetY = 15; // Vertical offset for labels from the axis line

    // Draw the main horizontal axis line
    ctx.beginPath();
    const startX = centerX - 2.5 * a_dist;
    const endX = centerX + 2.8 * a_dist;
    ctx.moveTo(startX, centerY);
    ctx.lineTo(endX, centerY);
    ctx.stroke();

    // Draw the arrowhead at the right end of the axis
    ctx.beginPath();
    ctx.moveTo(endX, centerY);
    ctx.lineTo(endX - 15, centerY - 8);
    ctx.moveTo(endX, centerY);
    ctx.lineTo(endX - 15, centerY + 8);
    ctx.stroke();

    // Define the points and their labels
    const points = [
        { pos: -2 * a_dist, charge: '-8Q', coord: '-2a' },
        { pos: -a_dist,     charge: 'Q',     coord: '-a'  },
        { pos: 0,           charge: 'q, m',  coord: 'O'   },
        { pos: a_dist,      charge: 'Q',     coord: 'a'   },
        { pos: 2 * a_dist,  charge: '-8Q',   coord: '2a'  }
    ];

    // Set font for mathematical variables
    ctx.font = 'italic 22px "Times New Roman"';

    // Iterate through points to draw tick marks and labels
    points.forEach(point => {
        const x = centerX + point.pos;

        // Draw the vertical tick mark
        ctx.beginPath();
        ctx.moveTo(x, centerY - tickHeight);
        ctx.lineTo(x, centerY + tickHeight);
        ctx.stroke();

        // Draw the charge label (above the axis)
        ctx.textBaseline = 'bottom';
        // Special case for 'q, m' to not be italic
        if (point.charge === 'q, m') {
            ctx.font = 'italic 22px "Times New Roman"';
            ctx.fillText('q', x - 10, centerY - labelOffsetY);
            ctx.font = '22px "Times New Roman"';
            ctx.fillText(',', x, centerY - labelOffsetY);
            ctx.font = 'italic 22px "Times New Roman"';
            ctx.fillText('m', x + 12, centerY - labelOffsetY);
        } else {
             ctx.fillText(point.charge, x, centerY - labelOffsetY);
        }

        // Draw the position label (below the axis)
        ctx.textBaseline = 'top';
        // 'O' for origin is not italic
        if (point.coord === 'O') {
            ctx.font = '22px "Times New Roman"';
            ctx.fillText(point.coord, x, centerY + labelOffsetY);
            ctx.font = 'italic 22px "Times New Roman"'; // Reset for next iteration
        } else {
            ctx.fillText(point.coord, x, centerY + labelOffsetY);
        }
    });

    // Draw the caption below the diagram
    ctx.font = '20px "SimSun", "STSong"'; // Use a font that supports Chinese characters
    ctx.textBaseline = 'top';
    ctx.fillText('电图 1.3.1', centerX, centerY + 80);

</script>
</body>
</html>